package cn.zyjblogs.server.user.dto;

import cn.zyjblogs.server.user.po.OauthUserDetails;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Collection;
import java.util.Collections;

/**
 * @author zhuyijun
 */
public class AuthorizationDto implements Authentication {
    private String id;
    private String name;
    private String username;
    private Object principal;
    private String credentials;
    private Collection<SimpleGrantedAuthority> authorities;
    private Object details;
    private Boolean authenticated;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities == null ? Collections.emptyList() : authorities;
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getDetails() {
        return details;
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    @Override
    public boolean isAuthenticated() {
        return authenticated;
    }

    @Override
    public void setAuthenticated(boolean b) throws IllegalArgumentException {
        this.authenticated = b;
    }

    @Override
    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPrincipal(Object principal) {
        this.principal = principal;
    }

    public void setCredentials(String credentials) {
        this.credentials = credentials;
    }

    public void setAuthorities(Collection<SimpleGrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setDetails(Object details) {
        this.details = details;
    }

    public Boolean getAuthenticated() {
        return authenticated;
    }

    public void setAuthenticated(Boolean authenticated) {
        this.authenticated = authenticated;
    }

    public AuthorizationDto createAuthorization() {
        AuthorizationDto authorizationDto = new AuthorizationDto();
        OauthUserDetails oauthUserDetails = new OauthUserDetails();
        oauthUserDetails.setId(BaseContext.getUserId());
        oauthUserDetails.setName(BaseContext.getName());
        oauthUserDetails.setUsername(BaseContext.getUsername());
        oauthUserDetails.setTenantId(BaseContext.getTenantId());
        authorizationDto.setPrincipal(oauthUserDetails);
        authorizationDto.setAuthenticated(true);
        return authorizationDto;
    }
}
